import java.util.Map;
import java.util.TreeMap;

/**
 * 二叉搜索树
 */
class BinarySearchTree{
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val=val;
        }
    }

    /**
     * 查找val在不在当前搜索树中
     * 存在 返回cur
     * 不存在 返回return
     * @param val
     * @return
     */
    public TreeNode root=null;
    public TreeNode search(int val){
        TreeNode cur=root;
        while (cur!=null){
            if(cur.val<val){
                cur=cur.right;
            } else if (cur.val>val) {
                cur=cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }

    public boolean Insert(int val){
        TreeNode cur=root;
        TreeNode p=null;
        if(root==null){
            root=new TreeNode(val);
            return true;
        }
        while(cur!=null){
            if(cur.val<val){
                p=cur;
                cur=cur.right;
            } else if (cur.val>val) {
                p=cur;
                cur=cur.left;
            }else{
                //找到了一个val，相同的val不能进行插入
                return false;
            }
        }
        TreeNode node=new TreeNode(val);
        if(val<p.val){
            p.left=node;
        }else{
            p.right=node;
        }
        return true;
    }
    public void inorder(TreeNode root){
        if(root==null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }

    /**
     * 删除
     * @param key
     */
    public void remove(int key){
        TreeNode cur=root;
        TreeNode p=null;
        while(cur!=null){
            if(cur.val==key){
                removeNode(p,cur);
            }else if(cur.val<key){
                p=cur;
                cur=cur.right;
            }else {
                p=cur;
                cur=cur.left;
            }
        }
    }
    private void removeNode(TreeNode p,TreeNode cur){
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            } else if (cur==p.left) {
                p.left=cur.right;
            }else{
                p.right=cur.right;
            }
        } else if (cur.right==null) {
            if(cur==root){
                root=cur.left;
            } else if (cur==p.left) {
                p.left=cur.left;
            }else{
                p.right=cur.left;
            }
        } else {
            TreeNode target=cur.right;
            TreeNode targetP=cur;
            while(target.left!=null){
                targetP=target;
                target=target.left;
            }
            cur.val=target.val;
            if(target==targetP.left){
                targetP.left=target.right;
            }else{
                targetP.right=target.right;
            }

        }
    }
}
class Student{

}
public class Test {
    public static void main(String[] args) {
        Map<String,Integer>map=new TreeMap<>();
        map.put("hello",2);
        map.put("abc",4);
        int v=map.get("hello");
        System.out.println(v);

    }
    public static void main1(String[] args) {
        BinarySearchTree binarySearchTree=new BinarySearchTree();
        int[] array={5,3,4,1,7,8,2,6,0,9};
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.Insert(array[i]);
        }
        binarySearchTree.inorder(binarySearchTree.root);
        binarySearchTree.search(2);


    }
}
